/* eslint-disable no-shadow */
/* eslint-disable @typescript-eslint/no-unused-vars */
/**
 * Created by zhangsong on 2019/9/23.
 */
import globalConfig from '@/constants/config';
import { routerRedux } from 'dva/router';
import { formatMessage } from '@/utils/i18n';
import StaffReducer from '@/dva/application/staff';
import * as staffApi from '@/services/application/staff';
import Modal from '@/components/Modal';
import { StaffApplication } from '@/constants/routes';
import { defaultPaginationParam } from '@/constants/styleConfig';
// import { mapToSearch } from '@/utils/utils';
import URI from '@/services/uri';
import _ from 'lodash';

const urly = URI.application;

const { Namespace, State, Reducers, Effects } = StaffReducer;

const Model = {
  namespace: Namespace,
  state: State,
  reducers: {
    [Reducers.setState](state, { data = {} } = {}) {
      // console.log('来了吗');
      return {
        ...state,
        ...data,
      };
    },
  },
  effects: {
    *[Effects.approval]({ data = {} }, { put, call }) {
      yield call(staffApi.approval, data.id);
      yield Modal.tips(formatMessage('baseInfo.operatorSuccess'));
      yield put({
        type: Effects.getStaffById,
        data: data.id,
      });
    },
    *[Effects.disapproval]({ data }, { put, call }) {
      yield call(staffApi.disapproval, data.id);
      yield Modal.tips(formatMessage('baseInfo.operatorSuccess'));
      yield put({
        type: Effects.getStaffById,
        data: data.id,
      });
    },
    *[Effects.reviewok]({ data }, { put, call }) {
      yield call(staffApi.reviewokstaff, data.id);
      yield Modal.tips(formatMessage('baseInfo.operatorSuccess'));
      yield put({
        type: Effects.getStaffById,
        data: data.id,
      });
    },
    *[Effects.saveStaff]({ data }, { put, call }) {
      yield put({
        type: Reducers.setState,
        data: {
          saveLoading: true,
        },
      });
      try {
        const response = yield call(staffApi.saveStaff, data);
        console.log('提交的参数信息', response);
        if (response.code !== 200) {
          Modal.error({
            title: formatMessage('baseInfo.errorTitle'),
            content: _.get(response, 'blackMsg', response.msg),
          });
          yield put({
            type: Reducers.setState,
            data: {
              saveLoading: false,
              saveSuccessModal: false,
              pdfReview: [],
            },
          });
        } else {
          yield put({
            type: Reducers.setState,
            data: {
              saveLoading: false,
              saveSuccessModal: true,
              pdfReview: [],
            },
          });
        }
      } catch (e) {
        Modal.error({
          title: formatMessage('baseInfo.errorTitle'),
          content: e.message,
        });
      }
      yield put({
        type: Reducers.setState,
        data: {
          saveLoading: false,
        },
      });
    },

    *[Effects.getStaffById]({ data }, { put, call }) {
      yield put({
        type: Reducers.setState,
        data: {
          detailLoading: true,
          staffInfo: {},
        },
      });
      try {
        const response = yield call(staffApi.getById, data);
        console.log('看看这个获取详情的', response)
        yield put({
          type: Reducers.setState,
          data: {
            detailLoading: false,
            staffInfo: response,
          },
        });
      } catch (e) {
        yield Modal.error({
          title: formatMessage('baseInfo.errorTitle'),
          content: JSON.stringify(formatMessage('applicationCommon.workerCard.errorcontext')),
        });
        yield put(routerRedux.push(StaffApplication.list.url));
      }
    },

    *[Effects.getPreviewPdfById]({ data }, { put, call }) {
      yield put({
        type: Reducers.setState,
        data: {
          pdfLoading: true,
          pdfPreviewVisible: true,
        },
      });
      try {
        const response = yield call(staffApi.getPDFById, data.id);
        yield put({
          type: Reducers.setState,
          data: {
            pdfLoading: false,
            pdfReview: response.map(item => `${globalConfig.host}${globalConfig.baseAPI}${item}`),
          },
        });
      } catch (e) {
        console.error(e);
      }
    },
    *[Effects.handPDFModalCancel]({ data }, { put, call }) {
      yield put({
        type: Reducers.setState,
        data: {
          pdfPreviewVisible: false,
          pdfReview: [],
        },
      });
    },
    *[Effects.downLoadXlsx](_, { put, call }) {
      yield put({
        type: Reducers.setState,
        data: {
          xslloading: true,
        },
      });
      const response = yield call(staffApi.staffDownXlsx);
      console.log('进来了', response);
      // 下载按钮点击事件
      const link = document.createElement('a'); // 创建a标签
      //   link.style.display = 'none';
      link.href = urly.staffUrl.downxlsx; // 设置a标签路径
      link.download = 'download.xlsx'; // 设置文件名， 也可以这种写法 （link.setAttribute('download', '名单列表.xls');
      document.body.appendChild(link);
      link.click();
      URL.revokeObjectURL(link.href); // 释放 URL对象
      document.body.removeChild(link);
      yield put({
        type: Reducers.setState,
        data: {
          xslloading: false,
        },
      });
    },
    *[Effects.listByParam]({ data = {} }, { put, call }) {
      const {
        page = defaultPaginationParam.page,
        pageSize = defaultPaginationParam.pageSize,
        query = {},
      } = data;
      yield put({
        type: Reducers.setState,
        data: {
          listLoading: true,
        },
      });
      try {
        const response = yield call(staffApi.listByParam, {
          page_num: page,
          page_size: pageSize,
          ...query,
        });
        yield put({
          type: Reducers.setState,
          data: {
            listLoading: false,
            listData: response.data,
            query,
            page: response.page,
            pageSize: response.pageSize,
            total: response.total,
          },
        });
      } catch (e) {
        yield Modal.error({
          title: formatMessage('baseInfo.errorTitle'),
          content: JSON.stringify(formatMessage('applicationCommon.workerCard.errorcontext')),
        });
      }
    },

    *[Effects.modifyStaffById]({ data }, { put, call }) {
      const { body, id, page, pageSize, query = {} } = data;
      yield put({
        type: Reducers.setState,
        data: {
          saveLoading: true,
        },
      });
      try {
        const response = yield call(staffApi.updateById, {
          id,
          body,
        });
        if (response.code === 200) {
          yield put({
            type: Reducers.setState,
            data: {
              saveLoading: false,
              saveSuccessModal: true,
              pdfReview: [],
            },
          });
        } else {
          yield Modal.error({
            title: formatMessage('baseInfo.errorTitle'),
            content: JSON.stringify(response.msg),
          });
          yield put({
            type: Reducers.setState,
            data: {
              saveLoading: false,
            },
          });
        }
      } catch (e) {
        yield Modal.error({
          title: formatMessage('baseInfo.errorTitle'),
          content: JSON.stringify(formatMessage('applicationCommon.workerCard.errorcontext')),
        });
        yield put({
          type: Reducers.setState,
          data: {
            saveLoading: false,
          },
        });
      }
    },
    // 註銷彈窗
    *[Effects.openCallModal]({ data }, { put, call }) {
      yield put({
        type: Reducers.setState,
        data: {
          callMadal: data,
        },
      });
    },
    // 更新註銷過期時間
    *[Effects.updateCallDate]({ data }, { put, call }) {
      console.log('进来了', data)
      yield put({
        type: Reducers.setState,
        data: {
          saveLoading: true,
        },
      });
      try {
        const response = yield call(staffApi.updateCallDate, data.update);
        console.log('員工的', response)
        yield put({
          type: Reducers.setState,
          data: {
            callMadal: false,
          },
        });
        if (response.result === 200) {
          yield put({
            type: Effects.listByParam,
            data: data.pagesize,
          });
          yield Modal.success({
            title: formatMessage('baseInfo.saveSuccess'),
            content: '',
          });
        }
        console.log('更新看看成功不', response)
      } catch {
        yield Modal.error({
          title: formatMessage('baseInfo.errorTitle'),
          content: '失敗',
        });
        yield put({
          type: Reducers.setState,
          data: {
            saveLoading: false,
          },
        });
      }
    },
    // 获取分组列表
    *[Effects.getGroupList](_, { put, call }) {
      yield put({
        type: Reducers.setState,
        data: {
          saveLoading: true,
        },
      });
      try {
        const response = yield call(staffApi.getGroupList);
        console.log('分组数据', response)
        const list = response.listNames.map(i => ({ value: i.name, label: i.name }))
        console.log('listlistlistlist', list)
        yield put({
          type: Reducers.setState,
          data: {
            saveLoading: false,
            groupList: list,
          },
        });
      } catch (e) {
        console.log('分组数据', e)
        // yield Modal.error({
        //   title: formatMessage('baseInfo.errorTitle'),
        //   content: '失敗',
        // });
        yield put({
          type: Reducers.setState,
          data: {
            saveLoading: false,
          },
        });
      }
    },
    // 修改分组列表
    *[Effects.creatGroupList]({ data }, { put, call }) {
      yield put({
        type: Reducers.setState,
        data: {
          saveLoading: true,
        },
      });
      try {
        const response = yield call(staffApi.creatGroupList, data);
        const list = response.listNames.map(i => ({ value: i, label: i }))
        console.log('新增分组数据', response, list)
        yield put({
          type: Reducers.setState,
          data: {
            saveLoading: false,
            groupList: list,
          },
        });
      } catch (e) {
        yield Modal.error({
          title: formatMessage('baseInfo.errorTitle'),
          content: '失敗',
        });
        yield put({
          type: Reducers.setState,
          data: {
            saveLoading: false,
          },
        });
      }
    },
    *[Effects.removeById]({ data }, { put, call }) {
      const { page, pageSize, query, id } = data;
      yield put({
        type: Reducers.setState,
        data: {
          listLoading: true,
        },
      });
      try {
        yield call(staffApi.removeById, id);
        yield Modal.success({
          title: formatMessage('baseInfo.removeSuccess'),
        });
      } catch (e) {
        yield Modal.error({
          title: formatMessage('baseInfo.errorTitle'),
          content: JSON.stringify(formatMessage('applicationCommon.workerCard.errorcontext')),
        });
      }
      yield put({
        type: Effects.listByParam,
        data: {
          page,
          pageSize,
          query,
        },
      });
    },
  },
};

export default Model;
